import {register} from "./user.js"  
var nameInp=document.getElementById("user_name")
    var namespan=nameInp.nextElementSibling;
    var pwdInp=document.getElementById("user_pwd")
    var pwdspan=pwdInp.nextElementSibling
    var phoneInp=document.getElementById("user_phone")
    var phonespan=phoneInp.nextElementSibling
    var btn=document.getElementById("button")

    nameInp.onblur=function(){
        var reg=/[\u4e00-\u9fa5\w]{2,12}/ig
        if(reg.test(nameInp.value)){
            namespan.innerHTML="√"
            namespan.style.color="green"
            return true
        }
        else{
            namespan.innerHTML="输入格式有误"
            namespan.style.color="red"
            return false
        }
    }

    pwdInp.onblur=function(){
        var reg=/\w{6,12}/ig
        if(reg.test(pwdInp.value)){
            pwdspan.innerHTML="√"
            pwdspan.style.color="green"
            return true
        }
        else{
            pwdspan.innerHTML="输入格式有误"
            pwdspan.style.color="red"
            return false
        }
    }

    phoneInp.onblur=function(){
        var reg=/\w{6,12}/ig
        if(reg.test(phoneInp.value)){
            phonespan.innerHTML="√"
            phonespan.style.color="green"
            return true
        }
        else{
            phonespan.innerHTML="输入格式有误"
            phonespan.style.color="red"
            return false
        }
    }
    btn.onclick=function(){
        if(nameInp.onblur()&&pwdInp.onblur()&&phoneInp.onblur()){
            register(nameInp.value,pwdInp.value,phoneInp.value).then(function(arr){
                console.log(arr)
           if(arr.status){
               alert("注册成功")
           }
           else{
               if(arr.err=="name"){
                namespan.innerHTML="用户名重复"
                namespan.style.color="red"
               }
               else{
                phonespan.innerHTML="手机号重复"
                phonespan.style.color="red"  
               }
           }
            })
        }
    }